<?php

namespace app\bews\controller;

use app\bews\model\Base;
use think\app;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;


class User extends Base
{
    public function userInfo(){
        $lists = Db::table("bew_user")->select();
        View::assign([
            'lists'=>$lists,
        ]);
        return View::fetch();
    }

    public function userAdd(){
        if(Request::isPost()){
//            print_r(input('post.'));
            $name = input('post.name');
            $password = input('post.password');
            $mobile = input('post.mobile');
            $birth = input('post.birth');
            $sex = input('post.sex');
            $addr = input('post.addr');
            $qq = input('post.qq');
            $email = input('post.email');
            $prov = input('post.prov');
            $city = input('post.city');
            $realname = input('post.realname');
            $weibo = input('post.weibo');
            $weixin = input('post.weixin');
            $company = input('post.company');

            $insert = Db::table('bew_user')->insert([
                'name'=>$name,
                'password'=>$password,
                'mobile'=>$mobile,
                'birth'=>$birth,
                'sex'=>$sex,
                'addr'=>$addr,
                'qq'=>$qq,
                'email'=>$email,
                'prov'=>$prov,
                'city'=>$city,
                'realname'=>$realname,
                'weibo'=>$weibo,
                'weixin'=>$weixin,
                'company'=>$company
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            return View::fetch();
        }
    }

    public function userModify(){
        if(Request::isPost()){
            $id = input('post.id');
            $name = input('post.name');
            $password = input('post.password');
            $mobile = input('post.mobile');
            $birth = input('post.birth');
            $sex = input('post.sex');
            $addr = input('post.addr');
            $qq = input('post.qq');
            $email = input('post.email');
            $prov = input('post.prov');
            $city = input('post.city');
            $realname = input('post.realname');
            $weibo = input('post.weibo');
            $weixin = input('post.weixin');
            $company = input('post.company');

            $update = Db::table("bew_user")->where('id',$id)->update([
                'name'=>$name,
                'password'=>$password,
                'mobile'=>$mobile,
                'birth'=>$birth,
                'sex'=>$sex,
                'addr'=>$addr,
                'qq'=>$qq,
                'email'=>$email,
                'prov'=>$prov,
                'city'=>$city,
                'realname'=>$realname,
                'weibo'=>$weibo,
                'weixin'=>$weixin,
                'company'=>$company
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            $id = input('get.id');
            if(empty($id)){
                $this->error("请传入id");
                exit();
            }
            $find = Db::table("bew_user")->where('id',$id)->find();
            View::assign([
                'find'=>$find
            ]);
            return View::fetch();
        }
    }

    public function userDel(){
        $id = input('post.id');
        if(empty($id)){
            $this->error("请传入id");
            exit();
        }
        $del = Db::table("bew_user")->where('id',$id)->delete();
        if($del == null){
            echo json_encode(['code'=>1,'msg'=>'删除失败,请重试']);
            exit();
        }
        echo json_encode(['code'=>0,'msg'=>'删除成功']);
        return View::fetch();
    }


}